import QtQuick
import QtQuick.Controls

// 串口选择
ComboBox {
  id: comboBox
  model: []
  currentIndex: 0
  onCurrentIndexChanged: {
    // 直接从model获取当前选中的文本
    var selectedText = currentIndex >= 0 && model.length > currentIndex ? model[currentIndex] : "";
    // console.log("选中端口:", selectedText);
  }
  Connections {
    target: FindCom
    function onComInfoListChanged(list) {
      // 保存当前选中的文本
      var currentSelected = comboBox.currentText;
      comboBox.model = list;
      // 如果之前有选择，尝试恢复到相同的选项
      if (currentSelected && list.indexOf(currentSelected) >= 0) {
        comboBox.currentIndex = list.indexOf(currentSelected);
      } else {
        comboBox.currentIndex = 0; // 只有在没有之前选择时才默认为第一个
      }
    }
  }
}